import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:wan_android/pages/page_collection.dart';
import 'package:wan_android/pages/page_login.dart';

///我的
class PagePerson extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return PagePersonState();
  }
}

/// 登陆成功之后 要返回 登陆者的名字
///
class PagePersonState extends State<PagePerson> {
  String _user = '请登录';

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    print('mine 初始化状态');
    _hanldeLoginMethod();
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build

    return Container(
      child: ListView(
        children: <Widget>[
          Container(
            child: Align(
              child: CircleAvatar(
                child: Icon(
                  Icons.person,
                ),
              ),
            ),
            height: 120.0,
          ),
          Container(
            child: RaisedButton(
              onPressed: () {
                _jumpToLogin();
              },
              child: Text(_user),
              color: Colors.blue,
            ),
            height: 40.0,
            margin: EdgeInsets.only(left: 5.0, right: 5.0),
          ),
          ListTile(
            leading: Icon(
              Icons.favorite,
              color: Colors.blue,
            ),
            title: Text('喜欢的文章'),
            trailing: Icon(
              Icons.chevron_right,
              color: Colors.blue,
            ),
            onTap: () {
              _jumpToCollectionPage();
            },
          ),
          ListTile(
            leading: Icon(
              Icons.toys,
              color: Colors.blue,
            ),
            title: Text('关于我们'),
            trailing: Icon(
              Icons.chevron_right,
              color: Colors.blue,
            ),
          ),
          ListTile(
            leading: Icon(
              Icons.computer,
              color: Colors.blue,
            ),
            title: Text('退出登录'),
            trailing: Icon(
              Icons.chevron_right,
              color: Colors.blue,
            ),
            onTap: () {
              _cleanMessage();
            },
          ),
        ],
      ),
    );
  }

  ///跳转到登陆页面
  _jumpToLogin() {
    Navigator.of(context).push(
      MaterialPageRoute(builder: (BuildContext context) {
        return LoginPage();
      }),
    ).then((value) {
      bool success = value;
      if (success) {
        _hanldeLoginMethod();
      }
    });
  }

  ///跳转到收藏界面
  _jumpToCollectionPage() async {
    SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
    String cookie = sharedPreferences.getString('cookie');
    if (cookie == null) {
      print('未保存cookie');
      return;
    } else {
      Navigator.of(context)
          .push(MaterialPageRoute(builder: (BuildContext context) {
        return CollectionPage();
      }));
    }
  }

  ///清楚登陆信息
  _cleanMessage() async {
    SharedPreferences sp = await SharedPreferences.getInstance();
    sp.clear();
    setState(() {
      _user = '请登录';
    });
  }

  /// 显示登陆者名字
  _hanldeLoginMethod() async {
    SharedPreferences sp = await SharedPreferences.getInstance();
    String name = sp.getString('userName');
    setState(() {
      _user = (name == null ? '请登录' : name);
    });
  }
}

String logoutApi = 'http://www.wanandroid.com/user/logout/json'; //退出
